define(function(require, exports, module) {
	/**
	 * HTML5  localStorage 本地存储
	 *   window.localStorage, 兼容 IE8+, firefox3.0+, chrome4.0+, opera10.5+
	 */
	var storage = window.localStorage;

    // 根据storage的key获取数据(object形式)
    function getDataStr(key) {
    	var empty = "";
        if (!key || !storage) {
            return empty;
        }
        return stripXssHtmlTag(storage.getItem(key)) || empty;
    }
	
	// 根据storage的key获取数据(object形式)
	function getData(key) {
		var dataStr = getDataStr(key);
		var empty = {};
		if (!dataStr) {
			return empty;
		}
		return JSON.parse(dataStr) || empty;
	}
	
	// 保存storage数据(自动转为)
	function setData(key, data) {
		if (!key || !storage) {
			return;
		}
		if (typeof(data) == 'undefined' || !data) {
			setItem(key, null);
			return;
		}
		if (typeof(data) !== 'string') {
			data = JSON.stringify(data);
		}
		setItem(key, data);
		
		// setItem时捕获异常，无痕模式会抛出QuoTaExceededError
		function setItem(k, v) {
			try {
				storage.setItem(k, v);
			} catch (error) {
				
			}
		}
	}
	
	// 更新数据中的某一属性值
	function setDataProperty(key, property, value) {
		if (!key || !property || !storage) {
			return;
		}
		var data = getData(key);
		data[property] = value || null;
		setData(key, data);
	}
	// 根据key删除本地存储数据	
	function removeData(key) {
		if (!key || !storage) {
			return;
		}
		storage.removeItem(key);
	}
	// 清空本地存储数据
	function clear() {
		if (!storage) {
			return;
		}
		storage.clear();
	}
    /**
     * 过滤可能的存在xss攻击的HTML标签(简单实现)
     * @param string _text - 传递进去的文本内容
     * @return string 返回替换后的结果
     */
	function stripXssHtmlTag(_text) {
        if (!_text){
            return _text;
        }
        // var _htmlTag = /<[\/|!]?(html|head|body|div|span|DOCTYPE|title|link|meta|style|p|h1|h2|h3|h4|h5|h6|strong|em|abbr|acronym|address|bdo|blockquote|cite|q|code|ins|del|dfn|kbd|pre|samp|var|br|a|base|img|area|map|object|param|ul|ol|li|dl|dt|dd|table|tr|td|th|tbody|thead|tfoot|col|colgroup|caption|form|input|textarea|select|option|optgroup|button|label|fieldset|legend|script|noscript|b|i|tt|sub|sup|big|small|hr)[^>]*>/ig;
        var _htmlTag = /<[\/|!]?(html|head|body|DOCTYPE|title|link|meta|style|var|base|object|param|script|noscript)[^>]*>/ig;
        return _text.replace(_htmlTag, "");
	}
	
	module.exports = {
		getData: getData,
		getDataStr: getDataStr,
		setData: setData,
		setDataProperty: setDataProperty,
		removeData: removeData,
		clear: clear
	};
});